<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends La_Controller {
	
	function index() {
		$this->page_title("搜索-行动网");
		
		$this->load->model(array('Emergency_model', 'City_model','User_model', 'Group_model', 'Expert_model', 'Comment_model'));
		$search_type = $this->input->get("search_type");
		$key = $this->input->get("key");
		$province_id = $this->input->get("province");
		$city_id = $this->input->get("city");
		$category_id = $this->input->get("category_id");
		$area_id = $this->input->get("area");
		if (empty($search_type)) {
			$search_type = 'story';
		}
		$offset = intval($this->uri->segment(3));
		$this->data['provinces'] = $this->City_model->find_all_province();
		$this->data['nav'] = $search_type;
		$this->data['key'] = $key;
		$this->load->library('pager');
		$config['base_url'] = site_url("search/index");
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 3;
		$config['suffix'] = '?search_type=' . $search_type . '&key=' . $key;
		$condition = "g.title like '%" . $key . "%'";
		if (!empty($province_id)) {
			$condition .= " and g.province_id=" . $province_id;
			$config['suffix'] .= '&province_id=' . $province_id;
		}
		if (!empty($category_id)) {
			$condition .= " and g.category_id=" . $category_id;
			$config['suffix'] .= '&category_id=' . $category_id;
		}
		if (!empty($city_id)) {
			$condition .= " and g.city_id=" . $city_id;
			$config['suffix'] .= '&city_id=' . $city_id;
		}
		if (!empty($area_id)) {
			$condition .= " and g.area_id=" . $area_id;
			$config['suffix'] .= '&area_id=' . $area_id;
		}
		switch($search_type) {
			case 'expert':
				$condition = "e.title like '%" . $key . "%'";
				if (!empty($province_id)) {
					$condition .= " and e.province_id=" . $province_id;
				}
				if (!empty($city_id)) {
					$condition .= " and e.city_id=" . $city_id;
				}
				if (!empty($area_id)) {
					$condition .= " and e.area_id=" . $area_id;
				}
				$score = $this->input->get("score");
				$is_vertification = $this->input->get("is_vertification");
				$birth_period = $this->input->get("birth_period");
				$sex = $this->input->get("sex");
				
				if (!empty($is_vertification)) {
					$condition .= " and u.is_vertification=" . ($is_vertification == 'yes' ? 1 : 0);
					$this->data['is_vertification'] = $is_vertification;
					$config['suffix'] .= '&is_vertification=' . $is_vertification;
				}
				
				if (isset($sex) && $sex !='') {
					$condition .= " and u.gender=" . $sex;
					$this->data['sex'] = $sex;
					$config['suffix'] .= '&sex=' . $sex;
				}
				
				if (!empty($birth_period)) {
					$condition .= " and u.birth_period='" . $birth_period . "'";
					$this->data['birth_period'] = $birth_period;
					$config['suffix'] .= '&birth_period=' . $birth_period;
				}
				if (!empty($score)) {
					$sql_count = "select count(*) as total from (select avg(c.score) as score,u.id from experts e 
					left join users u on u.id = e.created_by 
					left join comments c on c.target_id=e.created_by and c.target_type='user' 
				 	where 1=1 and " . $condition;
					$sql_count .= " group by e.created_by having score>=" . $score . ")a";
					
					$sql = "select avg(c.score) as score, e.id,e.title,e.price,e.province_id,e.city_id,e.area_id,e.created_by,u.nickname,u.thumb_avatar from experts e 
							left join users u on u.id=e.created_by
							left join comments c on c.target_id=e.created_by and c.target_type='user' 
							where 1=1 and " . $condition . " group by e.created_by having score>=" . $score;
					
					$this->data['score'] = $score;
				} else {
					$sql_count = "select count(*) as total from experts e 
					left join users u on u.id = e.created_by
				 	where 1=1 and " . $condition;
					
					$sql = "select e.id,e.title,e.price,e.province_id,e.city_id,e.area_id,e.created_by,u.nickname,u.thumb_avatar from experts e 
							left join users u on u.id=e.created_by
							where 1=1 and " . $condition;
				}
				$this->data['result_count'] = $this->Expert_model->get_count_by_condition_search($sql_count);
				$experts = $this->Expert_model->find_by_condition_search($sql, $offset, $config['per_page']);
				if (!empty($experts)) {
					$expert_string = '-1';
					foreach($experts as &$expert) {
						$expert_string .= "," . $expert['created_by'];
						$expert['score'] = 0;
						$expert['employ_count'] = 0;
						$expert['vertification'] = '';
					}
					unset($expert);
					$scores = $this->Comment_model->find_average_score($expert_string, 'user');
					for($i = 0, $n = count($scores); $i < $n; $i++) {
						foreach($experts as &$expert) {
							if ($expert['created_by'] == $scores[$i]['target_id']) {
								$expert['score'] = $scores[$i]['score'];
							}
						}
						unset($expert);
					}
					$emploies = $this->User_model->find_employ_count($expert_string);
					for($i = 0, $n = count($emploies); $i < $n; $i++) {
						foreach($experts as &$expert) {
							if ($expert['created_by'] == $emploies[$i]['user_id']) {
								$expert['employ_count'] = $emploies[$i]['total'];
							}
						}
						unset($expert);
					}
					$verts = $this->User_model->find_vertification_info($expert_string);
					for($i = 0, $n = count($verts); $i < $n; $i++) {
						foreach($experts as &$expert) {
							if ($expert['created_by'] == $verts[$i]['created_by']) {
							if($verts[$i]['total']) {
									$expert['vertification'] = "是";
								} else {
									$expert['vertification'] = "否";
								}
							}
						}
						unset($expert);
					}
				}
				
				$this->data['experts'] = $experts;
				break;
			case 'group':
				$condition .= " and g.status=1 ";
				$this->data['result_count'] = $this->Group_model->get_count_by_condition($condition);
				$this->data['groups'] = $this->Group_model->find_groups_by_condition($condition, $offset, $config['per_page']);
				
				break;
			case 'story':
			default:
				$bonus = $this->input->get("bonus");
				$score = $this->input->get("score");
				$is_vertification = $this->input->get("is_vertification");
				if (!empty($is_vertification)) {
					$condition .= " and u.is_vertification=" . ($is_vertification == 'yes' ? 1 : 0);
					$this->data['is_vertification'] = $is_vertification;
					$config['suffix'] .= '&is_vertification=' . $is_vertification;
				}
				if (!empty($bonus)) {
					$bonus_array = split(",", $bonus);
					$condition .= " and g.bonus>=" . $bonus_array[0] . " and g.bonus<" . $bonus_array[1];
					$this->data['bonus'] = $bonus;
					$config['suffix'] .= '&bonus=' . $bonus;
				}
				$condition .= " and g.status=1 ";
				$order_by = "order by g.created desc ";
				if (!empty($score)) {
					$sql_count = "select count(*) as total from (select avg(c.score) as score,u.id from emergencies g 
					left join users u on u.id = g.created_by 
					left join comments c on c.target_id=g.created_by and c.target_type='user' 
				 	where 1=1 and " . $condition;
					$sql_count .= " group by g.created_by having score>=" . $score . ")a";
					
					$sql = "select g.*,u.nickname from emergencies g
							left join users u on u.id=g.created_by
							left join comments c on c.target_id=g.created_by and c.target_type='user' 
							where 1=1 and " . $condition . " group by g.created_by having score>=" . $score;
					
					$this->data['score'] = $score;
					$config['suffix'] .= '&score=' . $score;
				} else {
					$sql_count = "select count(*) as total from emergencies g
					left join users u on u.id = g.created_by
				 	where 1=1 and " . $condition;
					
					$sql = "select g.*,u.nickname from emergencies g
							left join users u on u.id=g.created_by
							where 1=1 and " . $condition;
				}
				$sql .= $order_by;
				$this->data['result_count'] = $this->Emergency_model->get_count_by_condition_search($sql_count);
				$this->data['stories'] = $this->Emergency_model->find_by_condition_search($sql, $offset, $config['per_page']);
				
			break;
		}		
		$config['total_rows'] = $this->data['result_count'];
		$this->pager->initialize($config);
		$this->data['pager'] =  $this->pager->create_links();
		$this->data['search_type'] = $search_type;
		$view = $search_type . "/search";
		$this->load->view($view, $this->data);
	}
}